package com.xiaobai.design_pattern.proxy;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * @author wangtw
 * @date 2023/12/1 23:10
 * @description
 */
public class GamePlayerProxy implements IGamePlayer{

    private IGamePlayer gamePlayer;

    public GamePlayerProxy(IGamePlayer gamePlayer) {
        this.gamePlayer = gamePlayer;
    }

    private void start() {
        System.out.println("开始时间为" + new Date());
    }

    private void end() {
        System.out.println("结束时间为" + new Date());
    }

    /**
     * 获取角色名称
     * @param operateStep
     */
    private void operatePerson(String operateStep) {
        Class<? extends IGamePlayer> aClass = gamePlayer.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            if (declaredField.getName().equals("name")) {
                declaredField.setAccessible(true);
                try {
                    Object name = declaredField.get(gamePlayer);
                    System.out.println(name + operateStep + "操作");
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void killBoss() {
        operatePerson("开始");
        start();
        this.gamePlayer.killBoss();
    }

    @Override
    public void upGrade() {
        this.gamePlayer.upGrade();
        operatePerson("结束");
        end();
    }
}
